package com.bill99.codecheck.sonar.service.impl;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.bill99.codecheck.sonar.orm.entity.RnmItem;
import com.bill99.codecheck.sonar.service.PerFormAntXmlService;
import com.bill99.riaframework.common.helper.Bill99Logger;

/**
 * 
 * * 执行build.xml文件
	 * @param build  build.xml文件
	 * @param level  日志输出级别(Project.MSG_INFO)
 *
 */
public class PerFormAntXmlServiceImpl implements PerFormAntXmlService {

	private Bill99Logger logger = Bill99Logger.getLogger(this.getClass());

	private String executePrefix;
	private String executeParamsAnt;
	private String executeCmdAnt;

	@Override
	public RnmItem perFormAntXml(String bulidPath) {
		RnmItem rnmItem = new RnmItem();
		Runtime runtime = Runtime.getRuntime();
		String errorMSG = "";
		String infoMSG = "";

		try {
			String[] args = new String[] { executePrefix, executeParamsAnt, executeCmdAnt + bulidPath };
			Process pro = runtime.exec(args);

			InputStreamReader reader = new InputStreamReader(pro.getInputStream());
			BufferedReader buReader = new BufferedReader(reader);
			String lineInfo = "";
			while ((lineInfo = buReader.readLine()) != null) {
				infoMSG += lineInfo + "\n";

			}
			logger.info(infoMSG);

			InputStream in = pro.getErrorStream();
			InputStreamReader isr = new InputStreamReader(in);
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null) {
				errorMSG += line + "\n";
			}
			logger.info(errorMSG);

			rnmItem.setErrMsg(errorMSG);
			rnmItem.setInfoMsg(infoMSG);
			//检查命令是否失败
			try {
				if (pro.waitFor() != 0) {
					logger.info("exit value:" + pro.exitValue());
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rnmItem;
	}

	//	@Override
	//	public void PerformAntXml(String bulidPath) {
	//		File buildFile = new File(bulidPath);
	//		Project p = new Project();
	//		//添加日志输出		
	//		DefaultLogger consoleLogger = new DefaultLogger();
	//		consoleLogger.setErrorPrintStream(System.err);
	//		consoleLogger.setOutputPrintStream(System.out);
	//		//输出信息级别
	//		consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
	//		p.addBuildListener(consoleLogger);
	//		try {
	//			p.fireBuildStarted();
	//			p.init();
	//			ProjectHelper helper = ProjectHelper.getProjectHelper();
	//			helper.parse(p, buildFile);
	//			p.executeTarget(p.getDefaultTarget());
	//			p.fireBuildFinished(null);
	//		} catch (BuildException e) {
	//			p.fireBuildFinished(e);
	//			e.printStackTrace();
	//		}
	//	}

	public void setExecutePrefix(String executePrefix) {
		this.executePrefix = executePrefix;
	}

	public void setExecuteParamsAnt(String executeParamsAnt) {
		this.executeParamsAnt = executeParamsAnt;
	}

	public void setExecuteCmdAnt(String executeCmdAnt) {
		this.executeCmdAnt = executeCmdAnt;
	}
}
